package com.syq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syq.mapper.DtsAdminMapper;
import com.syq.pojo.DtsAdmin;
import com.syq.service.DtsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @ClassName: DtsAdminServiceImpl
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/25 14:57
 */

@Service
@Transactional
public class DtsAdminServiceImpl implements DtsAdminService {
    @Autowired
    private DtsAdminMapper dtsAdminMapper;

    @Override
    public IPage<DtsAdmin> findPageBy(Integer page, Integer limit, String username, String sort, String order) {
        return dtsAdminMapper.selectPage(new Page<>(page,limit),
                new QueryWrapper<DtsAdmin>()
                        .like((!StringUtils.isEmpty(username)),"username",username)
                        .orderBy(!StringUtils.isEmpty(sort),order.equalsIgnoreCase("Asc"),sort));
    }

    @Override
    public void updateAdmin(DtsAdmin dtsAdmin) {
         dtsAdminMapper.update(dtsAdmin,new QueryWrapper<DtsAdmin>().eq("id",dtsAdmin.getId()));
    }

    @Override
    public DtsAdmin findById(Integer id) {
        return dtsAdminMapper.selectById(id);
    }

    @Override
    public void addAdmin(DtsAdmin dtsAdmin) {
        dtsAdminMapper.insert(dtsAdmin);
    }

    @Override
    public DtsAdmin findByName(String username) {
        return dtsAdminMapper.selectOne(new QueryWrapper<DtsAdmin>().eq("username",username));
    }

    @Override
    public void delete(Integer id) {
        dtsAdminMapper.deleteById(id);
    }

}
